<script setup lang="ts">
import {Lock, User} from "@element-plus/icons-vue";
import {reactive,ref} from "vue";
import useUserStore from '@/store/modules/user.ts'
import {useRouter,useRoute} from "vue-router";
import { ElNotification } from "element-plus";
import {getTime} from '@/utils/time.ts'
const $router = useRouter()
const $route = useRoute()
const useStore = useUserStore()
//登录信息
const loginForm = reactive({username:'admin',password:'atguigu123'})
// 表单实例的应用
const loginFormIns= ref()
//控制按钮加载效果
const loading = ref(false)
/**
 * 登录功能
 */
const login = async ()=>{
  await loginFormIns.value?.validate()
  loading.value=true
  try {
    await useStore.userLogin(loginForm)
    const redirect:any = $route.query.redirect
    $router.push({path:redirect||'/'})
    ElNotification({
      type:'success',
      message:'登录成功！',
      title:`Hi，${getTime()}`
    })
    loading.value=false
  }catch (e){
    setTimeout(function (){},2000)
    ElNotification({
      type:'error',
      message:(e as Error).message
    })
    loading.value=false
  }
}
/**
 * 账号格式校验
 * @param rule
 * @param value
 * @param callback
 */
const validatorUserName=(_:any,value:any,callback:any)=>{
  if(value.length>=5){
    callback()
  }else{
    callback(new Error('账号长度至少五位'))
  }
}
/**
 *
 * @param rule
 * @param value
 * @param callback
 */
const validatorPassword=(_:any,value:any,callback:any)=>{
  if(value.length>=6){
    callback()
  }else{
    callback(new Error('密码长度至少五位'))
  }
}
//定义表单校验规则配置对象
const rules = {
  username:[{trigger:'blur',validator:validatorUserName}],
  password:[{trigger:'change',validator:validatorPassword}]
}

</script>

<template>
  <div class="login_container">
    <el-row>
      <el-col :span="12"  :xs="0">
      </el-col>
      <el-col :span="12" :xs="24">
        <el-form class="login_form" :model="loginForm" :rules="rules" ref="loginFormIns">
          <h2>Hello</h2>
          <h3>欢迎来到硅谷甄选</h3>
          <el-form-item prop="username">
            <el-input :prefix-icon="User" placeholder="请输入账号" v-model="loginForm.username"></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input :prefix-icon="Lock" placeholder="请输入密码" v-model="loginForm.password" show-password></el-input>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" class="login_btn" @click="login" :loading="loading" :disabled="loading">登录</el-button>
          </el-form-item>
        </el-form>
      </el-col>
    </el-row>
  </div>
</template>

<style scoped lang="scss">
.login_container{
  width: 100%;
  height: 100vh;
  background: url("@/assets/images/background.jpg") no-repeat;
  background-size: cover;

  .login_form{
    width: 80%;
    margin-top: 25vh;
    background: url("@/assets/images/login_form.png") no-repeat;
    background-size: cover;
    padding: 45px 35px;
  }
  h2{
    color: white;
    font-size: 40px;
  }
  h3{
    color: white;
    font-size: 20px;
    margin:15px 0;
  }
  .login_btn{
    width: 100%;
  }
}
</style>